package de.pixelate.flixelprimer
{
	import com.insominio.IconTop;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	
	import flashx.textLayout.elements.TextFlow;
	
	import org.flixel.*;
	
	public class PlayState extends FlxState
	{
		[Embed(source="../../../../assets/mp3/ExplosionShip.mp3")]
		private var SoundExplosionShip:Class;
		[Embed(source="../../../../assets/mp3/ExplosionAlien.mp3")]
		private var SoundExplosionAlien:Class;
		[Embed(source="../../../../assets/mp3/Bullet.mp3")]
		private var SoundBullet:Class;
		
		[Embed(source="../../../../assets/png/IconRicochet.png")]
		private var ImgIconRicochet:Class;
		[Embed(source="../../../../assets/png/IconClusterBomb.png")]
		private var ImgIconClusterBomb:Class;
		
		private var _iconTopRicochet:IconTop;
		private var _iconTopClusterBomb:IconTop;
		
		private var _ship:Ship;
		
		private var _powerUpRicochet:PowerUpRicochet;
		private var _powerUpClusterBomb:PowerUpClusterBomb;
		
		private var _ricochet:Ricochet;
		private var _clusterBomb:ClusterBomb;
		
		private var _aliens:FlxGroup;
		
		public var _bullets:FlxGroup;
		
		private var _spawnTimer:Number;
		private var _spawnInterval:Number = 2.5;
		private var _scoreText:FlxText;
		private var _timeRemaining:FlxText;
		private var _gameOverText:FlxText;
		
		private var _powerUpRicochetTimer:Number;
		private var _powerUpRicochetInterval:Number = 15;
		
		private var _currentWeaponTime:Number = 5;
		
		private var _currentWeapon:String;
		public static const BULLET:String = "bullet";
		public static const RICOCHET:String = "ricochet";
		public static const CLUSTERBOMB:String = "clusterBomb";
		
		override public function create():void
		{
			bgColor = 0xFFABCC7D;
			
			_ship = new Ship();
			add(_ship);
			
			_aliens = new FlxGroup();
			add(_aliens);
			
			_bullets = new FlxGroup();
			add(_bullets);
			
			_currentWeapon = new String();
			_currentWeapon = BULLET;
						
			resetSpawnTimer();
			resetPowerUpRicochet();
			
			FlxG.score = 0;
			_scoreText = new FlxText(10, 8, 200, "0");
			_scoreText.setFormat(null, 32, 0xFF597137, "left");
			add(_scoreText);
			
			_timeRemaining = new FlxText(47, 50, 200, "0");
			_timeRemaining.setFormat(null, 24, 0xFF597137, "left");
			_timeRemaining.visible = false;
			add(_timeRemaining);
			
			_iconTopRicochet = new IconTop(12, 53, ImgIconRicochet);
			_iconTopRicochet.alpha = 0.5;
			add(_iconTopRicochet)
			
			_iconTopClusterBomb = new IconTop(12, 85, ImgIconClusterBomb);
			add(_iconTopClusterBomb);
			
			_powerUpClusterBomb = new PowerUpClusterBomb(FlxG.width, FlxG.height / 2);
			add(_powerUpClusterBomb);
			
			super.create();;
		}
		
		override public function update():void
		{
			switch (_currentWeapon)
			{
				case BULLET:
					fire(spawnBullet);
					break;
				case RICOCHET:
					fire(spawnRicochet);
					break;
				case CLUSTERBOMB:
					fire(spawnClusterBomb);
					break
			}
			
			currentWeaponTimer();
			
			_spawnTimer -= FlxG.elapsed;
			if(_spawnTimer < 0)
			{
				spawnAlien();
				resetSpawnTimer();
			}
			
			_powerUpRicochetTimer -= FlxG.elapsed;
			if(_powerUpRicochetTimer < 0)
			{
				spawnPowerUpRicochet();
				resetPowerUpRicochet();
			}
			
			FlxU.overlap(_aliens, _bullets, overlapAlienBullet);
			FlxU.overlap(_aliens, _ship, overlapAlienShip);
			
			FlxU.overlap(_ship, _powerUpRicochet, overlapShipPowerUpRicochet);
			FlxU.overlap(_ship, _powerUpClusterBomb, overlapShipPowerUpClusterBomb);
			
			if(FlxG.keys.ENTER && _ship.dead)
			{
				FlxG.state = new PlayState();
			}
			
			_timeRemaining.text = Math.floor(_currentWeaponTime + 1).toString();
			
			super.update();
		}
		
		
		//----------------------------------
		//  spawnAlien
		//----------------------------------
		
		/**
		 *  @private
		 */
		private function spawnAlien():void
		{
			var x:Number = FlxG.width;
			var y:Number = Math.random() * (FlxG.height - 100) + 50;
			_aliens.add(new Alien(x, y));
		}
		
		//----------------------------------
		//  resetSpawnTimer
		//----------------------------------
		
		/**
		 *  @private
		 */
		private function resetSpawnTimer():void
		{
			_spawnTimer = _spawnInterval;
			_spawnInterval *= 0.95;
			
			if(_spawnInterval < 0.1)
			_spawnInterval = 0.1;
		}
		
	    
	    //----------------------------------
	    //  spawnPowerUpRicochet
	    //----------------------------------
	
	    /**
	     *  @private
	     */
	    private function spawnPowerUpRicochet():void
	    {
			var min:uint = 40;
			var max:uint = FlxG.height - 40;
			_powerUpRicochet = 
				new PowerUpRicochet(FlxG.width, Math.random() * (max - min) + min);
			add(_powerUpRicochet);
	    }
		
		
		//----------------------------------
	    //  resetPowerUpRicochet
	    //----------------------------------
	
	    /**
	     *  @private
	     */
	    private function resetPowerUpRicochet():void
	    {
			_powerUpRicochetTimer = _powerUpRicochetInterval;
			
			if (_powerUpRicochetInterval < 0.1)
			{
				_powerUpRicochetInterval = 0.1;
			}
	    }		
		
		
	    //----------------------------------
	    //  currentWeaponTimer
	    //----------------------------------
	
	    /**
	     *  @private
	     */
	    private function currentWeaponTimer():void
	    {     
			if(_currentWeapon != BULLET)
			{
				_currentWeaponTime -= FlxG.elapsed;
				if(_currentWeaponTime < -1)
				{
					_currentWeapon = BULLET;
					_timeRemaining.visible = false;
					_iconTopRicochet.alpha = 0.5;

				}
			}
	    }
		
		
		//----------------------------------
		//  fire
		//----------------------------------
		
		/**
		 *  @private
		 */
		private function fire(spawnWeapon:Function):void
		{
			if(FlxG.keys.justPressed("SPACE") && _ship.dead == false)
			{
				spawnWeapon(_ship.getBulletSpawnPosition());
				FlxG.play(SoundBullet);
			}
		}
		
		
		//----------------------------------
		//  spawnBullet
		//----------------------------------
		
		/**
		 *  @private
		 */
		private function spawnBullet(p:FlxPoint):void
		{
			var bullet:Bullet = new Bullet(p.x, p.y);
			_bullets.add(bullet);
		}
		
		
		//----------------------------------
		//  spawnRicochet
		//----------------------------------
		
		/**
		 *  @private
		 */
		private function spawnRicochet(p:FlxPoint):void
		{
			var ricochet:Ricochet = new Ricochet(p.x, p.y);
			var ricochetLeft:Ricochet = new Ricochet(p.x, p.y, Ricochet.LEFT);
			var ricochetRight:Ricochet = new Ricochet(p.x, p.y, Ricochet.RIGHT);
			_bullets.add(ricochet);
			_bullets.add(ricochetLeft);
			_bullets.add(ricochetRight);
		}
		
		
	    //----------------------------------
	    //  spawnClusterBomb
	    //----------------------------------
	
	    /**
	     *  @private
	     */
	    private function spawnClusterBomb(p:FlxPoint):void
	    {
			var clusterBomb:ClusterBomb = new ClusterBomb(p.x, p.y);
			_bullets.add(clusterBomb);
	    }
		
		//----------------------------------
		//  overlapAlienBullet
		//----------------------------------
		
		/**
		 *  @private
		 */
		private function overlapAlienBullet(alien:Alien, bullet:FlxSprite):void
		{
			alien.kill();
			bullet.kill();
			FlxG.score += 1;
			_scoreText.text = FlxG.score.toString();
			FlxG.play(SoundExplosionAlien);
			
			var emitter:FlxEmitter = createEmitter(Alien);
			emitter.at(alien);

		}
		
		
		//----------------------------------
		//  overlapAlienShip
		//----------------------------------
		
		/**
		 *  @private
		 */
		private function overlapAlienShip(alien:Alien, ship:Ship):void
		{
			ship.kill();
			alien.kill();
			FlxG.quake.start(0.01);
			FlxG.play(SoundExplosionShip);
			
			_gameOverText = new FlxText(0, FlxG.height / 2, FlxG.width, "Game Over\n Press Enter to Play Again");
			_gameOverText.setFormat(null, 16, 0xFF597137, "center");
			add(_gameOverText);
			
			var emitter:FlxEmitter = createEmitter(Ship);
			emitter.at(ship);
		}
		
		
		//----------------------------------
		//  overlapShipPowerUpRicochet
		//----------------------------------
		
		/**
		 *  @private
		 */
		private function overlapShipPowerUpRicochet(ship:Ship, powerUpRicochet:PowerUpRicochet):void
		{
			_currentWeapon = RICOCHET;
			_currentWeaponTime = 5;
			_powerUpRicochet.kill();
			_timeRemaining.visible = true;
			_iconTopRicochet.alpha = 1;
		}


	    //----------------------------------
	    //  overlapShipPowerUpClusterBomb
	    //----------------------------------
	
	    /**
	     *  @private
	     */
	    private function overlapShipPowerUpClusterBomb(ship:Ship, powerUpClusterBomb:PowerUpClusterBomb):void
	    {     
			_currentWeapon = CLUSTERBOMB;
			_currentWeaponTime = 5;
			_powerUpClusterBomb.kill();
			_timeRemaining.visible = true;
	    }
		
		//----------------------------------
		//  createEmitter
		//----------------------------------
		
		/**
		 *  @private
		 */
		//Ceate explosion after a bullet hit an Alien or after Ship hits an Alien
		private function createEmitter(shipClass:Class):FlxEmitter
		{
			var emitter:FlxEmitter = new FlxEmitter();
			emitter.gravity = 40;
			emitter.delay = 1;
			emitter.maxRotation = 0;
			
			if(shipClass == Ship)
				emitter.setXSpeed(-500, 500);
			else if (shipClass == Alien)
				emitter.setXSpeed(500, 800);
				
			emitter.setYSpeed(-500, 500);
			
			var particles:int = 10;
			for(var i:int = 0; i < particles; i++)
			{
				var particle:FlxSprite = new FlxSprite();
				particle.createGraphic(2, 2, 0xFF591737);
				particle.exists = false;
				emitter.add(particle);
			}
			
			emitter.start();
			add(emitter);
			return emitter;
		}
			
	}
}